from app import app
from flask_cors import CORS
from comm.config import Config
from views.user import user_blue
from flask import request,jsonify
from utils.myjwt import myjwt

#日志模块
import logging
from logging import FileHandler

app.config.from_object(Config)
CORS(app)

app.register_blueprint(user_blue,url_prefix="/user")

# 每次请求前验证token
@app.before_request
def before_request():
    #定义一个白名单
    whitelist = ["/user/sendcode","/user/create_imagecode","/user/login","/user/get_wb","user/weiboCallback","user/weibobind"]
    #获取当前的访问的url
    path = request.path
    #判断是否在白名单中，如果存在啥都不做
    if path not in whitelist:
        # print(request.headers)
        #如果不存在从头部获取token
        try:
            token = request.headers['Token']
            #验证token，1有效性验证  2过期时间
            flag = myjwt.check(token)
            if flag == False:
                #不通过返回401
                return jsonify({"code":401,'mes':"无权限访问"})
        except:
            return jsonify({"code":401,'mes':"无权限访问"})




if __name__=="__main__":
    handler = logging.FileHandler("logs/"+'log.txt')
    app.logger.addHandler(handler)
    app.run()